﻿using FunArts.EasyDevOps.Shared.Models.Enums;
using FunArts.EasyDevOps.Shared.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Shared.Models.SshClient.Command
{
    /// <summary>
    /// 加载环境变量
    /// </summary>
    public class SourceEnvCommand : ILinuxCommand
    {
        /// <summary>
        /// 对应/etc/profile.d/java_home.sh
        /// </summary>
        public string FileName { get; set; }  

        /// <summary>
        /// 构建完整的Bash命令（包含添加source和验证变量两步）
        /// </summary>
        public string BuildCommand(SystemVersionTypeEnum systemVersionType)
        {
            if (FileName.IsNullOrWhiteSpace())
            {
                throw new Exception("文件名不能为空！");
            }

            // 使用heredoc格式避免转义问题
            string bashScript = $@"
                # 1. 将source命令追加到~/.bashrc（如果不存在）
                grep -q 'source /etc/profile.d/{FileName}.sh' ~/.bashrc || echo 'source /etc/profile.d/{FileName}.sh' >> ~/.bashrc

                source ~/.bashrc";

            // 移除缩进保证命令格式正确
            bashScript = bashScript.Replace("        ", "");
            return bashScript.Trim();
        }
    }
}
